package imvu.rbox.object
{
	import anshe.object.ExTextField;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import imvu.rbox.event.ActionEvent;
	import imvu.rbox.global.GStatic;
	import imvu.rbox.struct.CGameDataInfo;
	import imvu.rbox.struct.CGamePlayerInfo;
	
	import org.dstart.object.memo.MemoObject;

	public class PlayerInfoObject extends Sprite
	{
		public var mcBox:MainGamePlay;		//	主展示窗口
		public var mcUFacePlace:MovieClip;	//	对象图像显示区块
		public var txtName:ExTextField;		//	显示玩家成绩
		public var txtScore:ExTextField;	//	玩家成绩
		public var btnTick:SimpleButton;	//	踢除玩家按钮
		public var mcClose:Sprite;			//	房间是否关闭标识
		public var mcFlag:Sprite;			//	是否准备完毕标识
		
		private var playerInfo:CGamePlayerInfo;	//	游戏玩家信息
		private var gameData:CGameDataInfo;		//	游戏数据信息
		private var faceLoader:UserFaceLoader;	//	用户头像信息对象
		
		private var m_seatID:Number;			//	当前坐位对应的坐位号
		
		public function PlayerInfoObject()
		{
			InitObject();
			InitListener();
		}
		
		public function InitGameBase():void
		{			
			InitObject(true);
			mcBox.InitGameBase();			
		}
		
		public function set enabled(flag:Boolean):void
		{
			this.btnTick.visible = flag;
			this.btnTick.mouseEnabled = flag;
			this.mcBox.mouseChildren = this.mcBox.mouseEnabled = flag;
		}
		
		//	房间是否关闭的标识
		public function set closed(flag:Boolean):void
		{
			this.mcClose.visible = flag;
			this.btnTick.visible = !flag;
		}
		
		public function get closed():Boolean
		{
			return this.mcClose.visible;
		}
		
		//	设置用户信息
		public function set info(uInfo:CGamePlayerInfo):void
		{
			//try
			//{
				if(!this.closed)
				{
					this.playerInfo = uInfo;
					if(uInfo)
					{
						// trace("当前用户:" + uInfo.userName + "刷新一次" + uInfo.isPower);			
						
						this.txtName.text = uInfo.userNick;
									
						this.score = uInfo.userScore;
						this.mcBox.isBomb = uInfo.isPower;
						
						uInfo.isPower = false;
						
						this.faceLoader.LoadFace(this.playerInfo.userFace);
						
						if(!GStatic.isGameStart)
						{
							this.isReady = this.playerInfo.isReady;
							// this.mcBox.InitGameBase();
						}
						
						this.mcBox.hotPercent = Math.floor(this.playerInfo.nowPower / GStatic.MAX_POWER * 100)
						this.mcBox.RushBgData(this.playerInfo.gameData);
						
						if(this.playerInfo.isGirl)
						{
							this.mcUFacePlace.gotoAndStop(2);
						}
						else
						{
							this.mcUFacePlace.gotoAndStop(3);
						}
					}
					else
					{						
						this.score = 0;
						this.isReady = false;
						this.enabled = false;
						this.mcBox.InitGameBase();
						this.faceLoader.RemoveFace();
						this.txtName.text = "";	
						this.isGameOver = false;	
						this.mcUFacePlace.gotoAndStop(1);
						this.mcUFacePlace.mouseChildren = this.mcUFacePlace.mouseEnabled = false;						
					}
				}
			//}
			//catch(e:*)
			//{
			//	trace("初始化 playerInfo 时出错 ");
			//}
		}
		
		public function get info():CGamePlayerInfo
		{
			return this.playerInfo;
		}	
		
		public function set isReady(flag:Boolean):void
		{
			this.mcFlag.visible = flag;
		}

		//	设置对象的坐位号
		public function set seatID(num:Number):void
		{
			this.m_seatID = num;
		}
		
		//	取得对象的坐位号
		public function get seatID():Number
		{
			return this.m_seatID;
		}
		
		public function set score(num:Number):void
		{
			txtScore.text = "   " + num;
		}
		
		public function set isGameOver(flag:Boolean):void
		{			
			this.mcBox.isGameOver = flag;
		}
		
		public function set isBomb(flag:Boolean):void
		{
			this.mcBox.isBomb = flag;			
		}
		
		
		public function DeleteListener():void
		{
			this.mcBox.DeleteListener();
			this.btnTick.addEventListener(MouseEvent.CLICK, onTickUserEvent);
			
			this.mcBox.doubleClickEnabled = true;
			this.mcBox.addEventListener(MouseEvent.DOUBLE_CLICK, onDBClick);

		}
		
		///////////////////////////////////////////////////////////////////////////

		private function InitObject(isRush:Boolean = false):void
		{			
			this.score = 0;			
			this.closed = false;
			this.enabled = false;
			this.isReady = false;
			
			this.txtName.size = 9;
			this.txtScore.size = 11;
			
			this.mcClose.mouseChildren = this.mcClose.mouseEnabled = false;			
			this.mcFlag.mouseChildren = this.mcFlag.mouseEnabled = false;
			
			if(!isRush)
			{
				MemoObject.create(this.btnTick, "Tick user");
			
				this.txtName.text = "";
				this.seatID = 0;				
				this.mcUFacePlace.gotoAndStop(1);
	
				faceLoader = new UserFaceLoader(this.mcUFacePlace.width, this.mcUFacePlace.height);
				faceLoader.x = this.mcUFacePlace.x;
				faceLoader.y = this.mcUFacePlace.y;
				this.addChild(faceLoader);
			}
			
			for(var i:int = 0; i < this.numChildren; i++)
			{
				var disp:DisplayObject = this.getChildAt(i);
				if(disp is Sprite)
				{
					Sprite(disp).mouseChildren = Sprite(disp).mouseEnabled = false;
				}
			}
		}
		
		private function InitListener():void
		{
			this.btnTick.addEventListener(MouseEvent.CLICK, onTickUserEvent);
			
			this.mcBox.doubleClickEnabled = true;
			this.mcBox.addEventListener(MouseEvent.DOUBLE_CLICK, onDBClick);
		}

		//	踢除玩家事件
		private function onTickUserEvent(e:MouseEvent):void
		{
			// trace("tick user:");
			var obj:Object = new Object();
			obj.userID = this.playerInfo.clientID;
			
			this.btnTick.visible = false;
			
			this.dispatchEvent(new ActionEvent(ActionEvent.TICK_USER, obj));
		}
			
		//	关闭/开启坐位事件
		private function onDBClick(e:MouseEvent):void
		{
			// trace("DBClick::" + this.name);	
			var obj:Object = new Object();
			obj.seatID = seatID;
			obj.isClose = closed;
			this.dispatchEvent(new ActionEvent(ActionEvent.CLOSE_SEAT, obj)); 
		}
	}
}